<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/7/24
 * Time: 14:59
 */
namespace common\components\services\restclient;

use common\components\helpers\Fun;
use common\components\helpers\Util;
use yii\base\Component;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\httpclient\Client;

class BaseRest extends Component
{
    /*
     * httpclient GET 请求
     * $url:API地址
     *
     * */
    public static function httpGet($url)
    {
        $client = new Client();
        $response = $client->createRequest()
            ->setMethod('get')
            ->setUrl($url)
            ->addHeaders(['content-type' => 'application/json'])
            ->send();
        if ($response->statusCode != 200) {
            $message = ArrayHelper::keyExists('message',Json::decode($response->content))?Json::decode($response->content)['message']:0;
            $code = ArrayHelper::keyExists('status',Json::decode($response->content))?Json::decode($response->content)['status']:0;
            throw new RestClientHttpException($message, $code);
        }
        /* return $response->content;
         */
        $rest_data = Json::decode($response->content);
        return $rest_data;

    }

    /*
     * httpclient POST 请求
     * $url:API地址
     * $data:POST提交的数据
     * */
    public static function httpPost($url, $data)
    {
        $client = new Client();
        $response = $client->createRequest()
            ->setMethod('post')
            ->setUrl($url)
            ->addHeaders(['content-type' => 'application/json'])
            ->setData($data)
            ->send();
        if ($response->statusCode != 200&&$response->statusCode!=422) {
            $message = ArrayHelper::keyExists('message',Json::decode($response->content))?Json::decode($response->content)['message']:0;
            $code = ArrayHelper::keyExists('status',Json::decode($response->content))?Json::decode($response->content)['status']:0;
            throw new RestClientHttpException($message, $code);
        }elseif($response->statusCode != 200&&$response->statusCode==422){
            $message_arr=[];
            $message_data=Json::decode($response->content);
            foreach($message_data as $v){
                $message_arr[]=$v;
            }
            $code = Json::decode($response->content)['status'];
            throw new RestClientHttpException(Util::array2text($message_arr), $code);
        }
        return $response->content;
    }

    /*
     * httpclient PUT 请求
     * $url:API地址
     * $data:POST提交的数据
     * */
    public static function httpPut($url, $data)
    {
        $client = new Client();
        $response = $client->createRequest()
            ->setMethod('put')
            ->setUrl($url)
            ->addHeaders(['content-type' => 'application/json'])
            ->setData($data)
            ->send();
         /*if ($response->statusCode != 200) {
             $message = ArrayHelper::keyExists('message',Json::decode($response->content))?Json::decode($response->content)['message']:0;
             $code = ArrayHelper::keyExists('status',Json::decode($response->content))?Json::decode($response->content)['status']:0;
             throw new RestClientHttpException($message, $code);
         }*/
        return $response->content;
    }

    /*
     * httpclient DELETE 请求
     * $url:API地址
     * $data:POST提交的数据
     * */
    public static function httpDelete($url, $data)
    {
        $client = new Client();
        $response = $client->createRequest()
            ->setMethod('delete')
            ->setUrl($url)
            ->addHeaders(['content-type' => 'application/json'])
            ->setData($data)
            ->send();
        if ($response->statusCode != 200) {
            $message = ArrayHelper::keyExists('message',Json::decode($response->content))?Json::decode($response->content)['message']:0;
            $code = ArrayHelper::keyExists('status',Json::decode($response->content))?Json::decode($response->content)['status']:0;
            throw new RestClientHttpException($message, $code);
        }
        return $response->content;
    }
}